﻿    var dtMenu, dt, menuID = $("#menuID").val();
        
    $(function () {
        initPager();
        showArticle();
    });

    function initPager() {
        dtMenu = $('#tableMenu').DataTable({
            "paging": false,
            "info": false,
            "ajax": {
                "url": 'Right/Handlers/Right_MenuHandler.ashx?type=list&st=2',
                "type": "POST",
                "dataSrc": "data",
                "data": function (d) {
                }
            },
            "columns": [
                    { "data": 'MenuID', "title": "MenuID", visible: false },
                    { "data": 'MenuNameEn', "title": "菜单名称" }
            ],
            "createdRow": function (row, data, dataIndex) {
                $(row).attr('id', data.MenuID);
            },
            "initComplete": function (settings, json) {
                //点击行事件
                $('#tableMenu tbody').on('click', 'tr', function () {
                    if (dtMenu.row(this).data() != null) {
                        menuID = dtMenu.row(this).data().MenuID;
                        dt.ajax.url('CMS/Handlers/CMS_ArticleHandler.ashx?type=list&menuID=' + menuID).load();
                    }
                });

                if (menuID == 0) {
                    $('#tableMenu tbody tr:first').click();
                }
                else {
                    $('#tableMenu tbody tr#' + menuID).click();
                }

            }
        });
    }

    function showArticle() {
        dt = $('#table').DataTable({
            "ajax": {
                "url": 'CMS/Handlers/CMS_ArticleHandler.ashx?type=list',
                "type": "POST",
                "dataSrc": "data",
                "data": function (d) {
                    d.MenuID = menuID;
                    d.Title = $("#Title").val();
                }
            },
            "columns": [
                    { "data": 'ID', "title": "ID" },
                    { "data": 'Title', "title": "标题" },
                    { "data": 'CreateTime', "title": "创建时间" },
                    { "data": 'IsPublish', "title": "是否发布", render: fYesNo },
                    { "data": 'PublishTime', "title": "发布时间" }
            ]
        });
    }

    function showContent(id) {
        $.post("CMS/Handlers/CMS_ArticleHandler.ashx?type=get", { ID: id }, function (data) {

            $("#modalContent").find(".modal-title").text(data.Title);
            $("#modalContent").find(".modal-body").html(data.Content);
            $("#modalContent").modal("show");

        }, "json");
    }

    function checkSelected() {
        if ($('#table tbody tr.selected').length > 0) {
            return true;
        }
        else {
            jalert("请选择一条数据！");
            return false;
        }
    }

    function refresh() {
        dt.ajax.reload(null, false);
    }

    $('#btnDelete').click(function () {
        checkSelected() && jconfirm("确定要删除吗？", function () {
            $.post("CMS/Handlers/CMS_ArticleHandler.ashx?type=delete", { ID: dt.row('.selected').data().ID }, function (data) {
                if (data) {
                    if (data.isSuccess) {
                        refresh()
                    } else {
                        jalert(data.message);
                    }
                }
            }, "json");
        });
    });

    $('#btnPublish').click(function () {
        if (checkSelected()) {
            if (!dt.row('.selected').data().IsPublish) {
                $.post("CMS/Handlers/CMS_ArticleHandler.ashx?type=publish", { ID: dt.row('.selected').data().ID }, function (data) {
                    if (data) {
                        if (data.isSuccess) {
                            refresh()
                        } else {
                            jalert(data.message);
                        }
                    }
                }, "json");
            }
            else {
                jalert("选择的公告已发布，请重新选择。");
            }
        }
    });

    $("#btnBaidu").click(function () {
        $.post("CMS/Handlers/CMS_ArticleHandler.ashx?type=baidu", { }, function (data) {
            if (data) {
                if (data.isSuccess) {
                    jalert(JSON.stringify(data));
                    refresh()
                } else {
                    jalert(data.message);
                }
            }
        }, "json");
    });

    $('#btnEdit').click(function () {
        checkSelected() && loadPage("CMS/CMS_ArticleItem.aspx?ID=" + dt.row('.selected').data().ID + '&MenuID=' + menuID);
    });

    $("#btnAdd").click(function () {
        if (menuID == 0) {
            jalert("请选择相应的文章分类再新增文章。");
        }
        else {
            loadPage('CMS/CMS_ArticleItem.aspx?MenuID=' + menuID);
        }
    });
